//
//  SMSRequest.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/26.
//

#import "Item1MediaUploadRequest.h"
#import <AFNetworking/AFURLRequestSerialization.h>


@interface ImageCompressor : NSObject
// 压缩图片到指定大小（单位：KB）
+ (NSData *)compressImage:(UIImage *)image toMaxFileSizeKB:(CGFloat)maxFileSizeKB;
@end
@implementation ImageCompressor

+ (NSData *)compressImage:(UIImage *)image toMaxFileSizeKB:(CGFloat)maxFileSizeKB {
    CGFloat compression = 0.9f; // 初始压缩质量
    CGFloat maxCompression = 0.1f; // 最小压缩质量
    NSData *imageData = UIImageJPEGRepresentation(image, compression);

    // 如果图片已经小于目标大小，直接返回
    if (imageData.length / 1024 <= maxFileSizeKB) {
        return imageData;
    }

    // 逐步降低质量，直到图片大小小于目标大小
    while (imageData.length / 1024 > maxFileSizeKB && compression > maxCompression) {
        compression -= 0.1f;
        imageData = UIImageJPEGRepresentation(image, compression);
    }

    // 如果质量压缩到最低仍然无法满足要求，则调整图片尺寸
    if (imageData.length / 1024 > maxFileSizeKB) {
        UIImage *resizedImage = [self resizeImage:image toMaxDimension:1024]; // 调整图片尺寸
        imageData = UIImageJPEGRepresentation(resizedImage, compression);
    }

    return imageData;
}

// 调整图片尺寸
+ (UIImage *)resizeImage:(UIImage *)image toMaxDimension:(CGFloat)maxDimension {
    CGFloat width = image.size.width;
    CGFloat height = image.size.height;

    // 计算缩放比例
    CGFloat scale = maxDimension / MAX(width, height);
    if (scale >= 1.0) {
        return image; // 如果图片尺寸已经小于目标尺寸，直接返回
    }

    // 计算新的尺寸
    CGSize newSize = CGSizeMake(width * scale, height * scale);

    // 绘制新图片
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return resizedImage;
}
@end












@implementation Item1MediaUploadRequest

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}

- (NSString *)requestUrl {
    //return [@"/sulitloanapi/slu/speeches" stringByAppendingString:self.commonQuery];
//    NSString *uri = [@"/sulitloanapi/slu/speeches" stringByAppendingString:self.commonQuery];
//    NSCharacterSet *allowedCharacters = [NSCharacterSet URLQueryAllowedCharacterSet];
//    return [uri stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacters];
    return [self requestEncodingUrl:@"/sulitloanapi/slu/speeches"];
}
- (id)requestArgument {
    return @{
        @"bagger": self.bagger ?: @"", // 图片来源:1相册 2:拍照上传
        @"scenery": self.scenery ?: @"", // 产品id
        @"peppers": self.peppers ?: @"", // 10:人脸自拍, 11身份证正面
//        @"karl": self.karl ?: @"",// 压缩500K以内的图片NSData 参考代码：[formData appendPartWithFileData:data name:key fileName:@"xx.jpg" mimeType:@"image/jpeg"];
        @"monthly": self.monthly ?: @"", //卡类型 UMID/SSS/TIN/PASSPORT/DRIVINGLICENSE/PRC/POSTAL/Voter/PhilHealth
        @"faithfully": @"", // 默认为空，
        @"energetic": @"energetic",
        @"petit": @"1", // 传1即可
    };
}

- (AFConstructingBlock)constructingBodyBlock {
    return ^(id<AFMultipartFormData> formData) {
        //NSData *data = UIImageJPEGRepresentation(self.image, 1);
        NSString *fileName = @"a.jpeg";
        NSString *name = @"karl";
        NSString *type = @"image/jpeg";
        NSData *imageData = [ImageCompressor compressImage:self.image toMaxFileSizeKB:500];
        //[formData appendPartWithFileData:self.karl name:name fileName:fileName mimeType:type];
        [formData appendPartWithFileData:imageData name:name fileName:fileName mimeType:type];
    };
    return nil;
}

@end
